package com.peza.postman.controller;

import java.io.IOException;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import javax.servlet.ServletContext;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.peza.postman.common.PropertyService;
import com.peza.postman.domain.MailRequest;
import com.peza.postman.domain.MailResponse;
import com.peza.postman.mail.MailCenter;
import com.peza.postman.util.HealthLocker;


@Controller
public class PostmanController {
	
	@Autowired
	private MailCenter mailcenter;
	
	@Autowired
	private PropertyService ps;
	
	@Autowired
	private ServletContext context;
	
	private Logger logger = Logger.getLogger(this.getClass());
	
	
	
	@RequestMapping (value="/info", method = RequestMethod.GET)
	public void version(HttpServletResponse response, HttpServletRequest request){
		logger.info("info request from : " + request.getRemoteAddr());
		String info = HealthLocker.getInstance().getInfo();
		printToStream(info, response);
	} 
	
	@RequestMapping (value="/send", method = RequestMethod.GET)
	public void sendget(HttpServletRequest request, HttpServletResponse response){
		printToStream("value = " + ps.getValue(), response );
	}
	
	
	@RequestMapping (value="/send", method = RequestMethod.POST)
	public String send(HttpServletRequest request, HttpServletResponse response){
		
		logger.info("send request from : " + request.getRemoteAddr());
		String email = request.getParameter("email");
		String user = request.getParameter("user");
		String errorpage = request.getParameter("errorpage");
		String returnpage = request.getParameter("returnpage");
		
		MailRequest mail = new MailRequest();
		mail.setContent(email);
		mail.setTo(ps.getProperty(user));

		logger.info(mail.toString());
		
		MailResponse mailresponse = mailcenter.send(mail);
		
		logger.info(mailresponse.toString());

		
		if (mailresponse.isSuccess()){
			HealthLocker.getInstance().addSuccess();
			return "redirect:" + returnpage;
		} else {
			HealthLocker.getInstance().addFail();
			return "redirect:" + errorpage;
		}
		
	} 
	
	
	private void printToStream(String content, HttpServletResponse response){
		
		ServletOutputStream outputStream = null;
		try {
			outputStream = response.getOutputStream();
			outputStream.write(content.getBytes("UTF8"), 0, content.getBytes("UTF8").length);
			outputStream.flush();
			outputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	private void getVitals(){
		
		
		logger.debug(context.getServerInfo());
		
		Properties props = System.getProperties();
		Set<Object> keys = props.keySet();
		for (Object k : keys){
			if (k instanceof String){
				String key = (String) k;
				String val = props.getProperty(key);
				//logger.debug(key + " : " + val);
			}	
		}
	}
	
	
	public static void main (String [] args){
		
		PostmanController pc = new PostmanController();
		pc.getVitals();
	}
	
	
}
